package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.modular.common.service.SysAuthorityService;
import cn.e4j.modular.crm.entity.GwfbT;
import cn.e4j.modular.crm.mapper.GwfbTMapper;
import cn.e4j.modular.crm.service.GwfbTService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yuan_rz
 * @since 2019-10-25
 */
@Service
public class GwfbTServiceImpl extends ServiceImpl<GwfbTMapper, GwfbT> implements GwfbTService {
    @Autowired
    public SysAuthorityService sysAuthorityService;

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwfblist(Page<Object> page, Dto dto) {

        List<Map<String, Object>> gwfbDtos = this.baseMapper.listGwfbPage(page, dto);

        return gwfbDtos;
    }

    @Transactional(readOnly = true)
    public GwfbT getByGwfbkey(String ent, String gwfbno, BigDecimal gwfbseq) {
        UpdateWrapper<GwfbT> gwfbTUpdateWrapper = new UpdateWrapper<>();
        gwfbTUpdateWrapper.eq("GWFBENT", ent);
        gwfbTUpdateWrapper.eq("GWFBNO", gwfbno);
        if(gwfbseq == null){
            gwfbseq = new BigDecimal(0);
        }
        gwfbTUpdateWrapper.eq("GWFBSEQ", gwfbseq);
        GwfbT gwfb = this.baseMapper.selectOne(gwfbTUpdateWrapper);
        return gwfb;
    }

    @Transactional
    @Override
    public void add(Dto dto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        BigDecimal GWFBSEQ=BigDecimal.valueOf(1);
        BigDecimal tmp = InvokeUtil.invokeBeanMethod("gwfbTMapper,getMax_GWFBSEQ", dto);
        if(tmp.compareTo(GWFBSEQ)>0){
            GWFBSEQ=tmp;
        }
        GwfbT gwfb = new GwfbT();
        gwfb.setGWFBENT(dto.getString("ENT"));
        gwfb.setGWFBNO(dto.getString("GWFBNO"));
        gwfb.setGWFBSEQ(GWFBSEQ);
        gwfb.setGWFB001(dto.getString("GWFB001"));
        gwfb.setGWFB002(dto.getString("GWFB002"));
        gwfb.setGWFB003(dto.getString("GWFB003"));
        gwfb.setGWFB004(dto.getString("GWFB004"));
        gwfb.setGWFB005(dto.getString("GWFB005"));
        gwfb.setGWFB100(dto.getString("GWFB100"));
        gwfb.setGWFB101(dto.getBigDecimal("GWFB101"));
        gwfb.setGWFB102(dto.getBigDecimal("GWFB102"));
        gwfb.setGWFB103(dto.getBigDecimal("GWFB103"));
        gwfb.setGWFB104(dto.getBigDecimal("GWFB104"));
        gwfb.setGWFB105(dto.getBigDecimal("GWFB105"));
        gwfb.setGWFBACTI(dto.getString("GWFBACTI"));
        gwfb.insert();
    }

    @Transactional
    @Override
    public void edit(Dto dto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        GwfbT gwfb = new GwfbT();
        gwfb.setGWFBMODU(shiroUser.getACCOUNT());
        UpdateWrapper<GwfbT> gwfbTUpdateWrapper = new UpdateWrapper<>();
        gwfbTUpdateWrapper.eq("GWFBENT", shiroUser.getDBC());
        gwfbTUpdateWrapper.eq("GWFBNO", dto.getString("GWFBNO"));
        gwfbTUpdateWrapper.eq("GWFBSEQ", dto.getString("GWFBSEQ"));
        gwfb.update(gwfbTUpdateWrapper);
    }
    @Transactional
    @Override
    public void delete(Dto dto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        GwfbT gwfb = new GwfbT();
        UpdateWrapper<GwfbT> gwfbTUpdateWrapper = new UpdateWrapper<>();
        gwfbTUpdateWrapper.eq("GWFBENT", shiroUser.getDBC());
        gwfbTUpdateWrapper.eq("GWFBNO", dto.getString("GWFBNO"));
        gwfbTUpdateWrapper.eq("GWFBSEQ", dto.getString("GWFBSEQ"));
        gwfb.delete(gwfbTUpdateWrapper);
    }

    @Transactional
    @Override
    public void delDataBatch(List<GwfbT> gwfbTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        for (GwfbT gwfbT : gwfbTList) {
            UpdateWrapper<GwfbT> gwfbTUpdateWrapper = new UpdateWrapper<>();
            gwfbTUpdateWrapper.eq("GWFBENT", shiroUser.getDBC());
            gwfbTUpdateWrapper.eq("GWFBNO", gwfbT.getGWFBNO());
            gwfbTUpdateWrapper.eq("GWFBSEQ", gwfbT.getGWFBSEQ());
            gwfbT.delete(gwfbTUpdateWrapper);
        }
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getlistPage(Page<Object> page, Dto dto) {
        if(dto.getString("hasPermission").equals("true")){
            dto.put("GWFBS", sysAuthorityService.getGwaesAuthority(dto));
        }

        List<Map<String, Object>> gwfbDtos = this.baseMapper.getlistPage(page, dto);

        return gwfbDtos;
    }

    @Transactional
    public void check(List<GwfbT> gwfbTList) {
        ShiroUser user = ShiroKit.getUser();
        for (GwfbT gwfb : gwfbTList) {
            UpdateWrapper<GwfbT> gwfbTUpdateWrapper = new UpdateWrapper<>();
            gwfbTUpdateWrapper.eq("GWFBENT", gwfb.getGWFBENT());
            gwfbTUpdateWrapper.eq("GWFBNO", gwfb.getGWFBNO());
            gwfbTUpdateWrapper.eq("GWFBSEQ", gwfb.getGWFBSEQ());
            gwfb.setGWFBACTI("Y");
            gwfb.setGWFBMODU(user.getID());
//            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyyMMdd");
//            gwfb.setGWDC014(fmt.format(LocalDate.now()));
            gwfb.update(gwfbTUpdateWrapper);
        }
    }

    @Transactional
    public void unCheck(List<GwfbT> gwfbTList) {
        ShiroUser user = ShiroKit.getUser();
        for (GwfbT gwfb : gwfbTList) {
            UpdateWrapper<GwfbT> gwfbTUpdateWrapper = new UpdateWrapper<>();
            gwfbTUpdateWrapper.eq("GWFBENT", gwfb.getGWFBENT());
            gwfbTUpdateWrapper.eq("GWFBNO", gwfb.getGWFBNO());
            gwfbTUpdateWrapper.eq("GWFBSEQ", gwfb.getGWFBSEQ());
            gwfb.setGWFBACTI("N");
            gwfb.setGWFBMODU(user.getID());
            gwfb.update(gwfbTUpdateWrapper);
        }
    }

    /**
     * 获取汇率
     * @return
     */
    @Transactional(readOnly = true)
    public Dto getHuiLv(Dto inDto) {
        Dto huiLv = this.baseMapper.getGwek005(inDto);
        if (huiLv == null) {
            huiLv = Dtos.newDto();
            huiLv.put("GWEK005",1);
        }
        return huiLv;
    }

}
